<template>
  <div class="profile-box">
    <!-- 未登录-头部 -->
    <div class="not-login login-bg" v-if="!user">
      <img src="@/assets/mobile.png" @click="$router.push('/login')" alt="" />
      <span class="not-logreq" @click="$router.push('/login')"
        >登录 / 注册</span
      >
    </div>
    <!-- 已登录-头部 -->
    <div class="login-info login-bg" v-if="user">
      <div class="top">
        <div class="imgbox">
          <van-image
            fit="cover"
            round
            :src="userInfo.photo"
            class="loginiImg"
          />
          <span class="loginText">{{ userInfo.name }}</span>
        </div>
        <div class="txtBox">
          <van-button round type="default" class="editBtn" to="/user"
            >编辑资料</van-button
          >
        </div>
      </div>
      <div class="bottom">
        <div class="gnBox">
          <span>{{ userInfo.art_count | getToFixed }}</span>
          <span>头条</span>
        </div>
        <div class="gnBox">
          <span>{{ userInfo.follow_count }}</span>
          <span>关注</span>
        </div>
        <div class="gnBox">
          <span>{{ userInfo.fans_count }}</span>
          <span>粉丝</span>
        </div>
        <div class="gnBox">
          <span>{{ userInfo.like_count }}</span>
          <span>获赞</span>
        </div>
      </div>
    </div>
    <!-- 收藏 - 历史 宫格导航 -->
    <van-grid clickable :column-num="2">
      <van-grid-item>
        <template #icon>
          <i class="toutiao toutiao-shoucang"></i>
        </template>
        <template #text>
          <span class="gridText">收 藏</span>
        </template>
      </van-grid-item>
      <van-grid-item>
        <template #icon>
          <i class="toutiao toutiao-lishi"></i>
        </template>
        <template #text>
          <span class="gridText">历 史</span>
        </template>
      </van-grid-item>
    </van-grid>
    <!-- 消息通知 - 小智同学 -->
    <div class="infoBox">
      <van-cell title="消息通知" is-link />
      <van-cell title="小智同学" is-link />
    </div>
    <!-- 退出 -->
    <van-cell class="quitBox" @click="quit" v-if="user" clickable>
      <template #title>
        <span>退出登录</span>
      </template>
    </van-cell>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user.js'
export default {
  data () {
    return {
      userInfo: {}
    }
  },
  created () {
    if (this.user) {
      this.loadUserInfo()
    }
  },
  computed: {
    ...mapState(['user'])
  },
  methods: {
    // 退出登录
    quit () {
      this.$dialog.confirm({
        title: '黑马头条',
        message: '确认退出该账号?'
      }).then(() => {
        // on close
        this.$store.commit('setUser', null)
      })
        .catch(() => {
          // on cancel
        })
    },
    // 登录获取用户信息
    async loadUserInfo () {
      try {
        const res = await getUserInfo()
        console.log(res)
        if (res.request.status !== 200) return this.$toast('获取数据失败,请稍后重试')
        this.userInfo = res.data.data
      } catch (err) {
        this.$toast('获取数据失败,请稍后重试')
      }
    }
  },
  filters: {
    getToFixed (value) {
      if (value > 10000) {
        return (value / 10000).toFixed(2) + '万'
      }
    }
  }
}
</script>

<style scoped lang='less'>
.profile-box {
  // 公共头部背景
  .login-bg {
    width: 750px;
    height: 400px;
    background-image: url(~@/assets/banner.png);
    background-size: cover;
  }
  // 未登录-头部
  .not-login {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    img {
      width: 132px;
      height: 132px;
    }
    .not-logreq {
      font-size: 28px;
      color: #fff;
      margin-top: 15px;
    }
  }
  // 已登录-头部
  .login-info {
    padding: 80px 32px 0 32px;
    box-sizing: border-box;
    color: #fff;
    .top {
      height: 160px;
      display: flex;
      justify-content: space-between;
      .imgbox {
        display: flex;
        flex: 1;
        justify-content: center;
        align-items: center;
        .loginiImg {
          width: 132px;
          height: 132px;
          border: 6px solid #fff;
          margin-right: 22px;
        }
        .loginText {
          width: 153px;
          height: 29px;
          font-size: 30px;
          color: #fff;
        }
      }
      .txtBox {
        flex: 1;
        display: flex;
        justify-content: flex-end;
        align-items: center;
        .editBtn {
          width: 120px;
          height: 50px;
          padding: 0;
          font-size: 20px;
          color: #666;
        }
      }
    }
    .bottom {
      display: flex;
      .gnBox {
        height: 160px;
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        span:nth-child(1) {
          font-size: 36px;
        }
        span:nth-child(2) {
          margin-top: 2px;
          font-size: 23px;
        }
      }
    }
  }
  //收藏历史
  .van-grid {
    .toutiao {
      font-size: 45px;
    }
    .toutiao-shoucang {
      color: #eb5253;
    }
    .toutiao-lishi {
      color: #ff9d1d;
    }
    .gridText {
      margin-top: 10px;
      font-size: 28px;
      color: #333333;
    }
  }
  // 消息通知
  .infoBox {
    margin-top: 10px;
  }
  .quitBox {
    margin-top: 10px;
    text-align: center;
    font-size: 30px;
    color: #d86262;
  }
}
</style>
